const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const connect = require("gulp-connect");
const sourcemaps = require("gulp-sourcemaps");
const concat = require("gulp-concat"); //合并模块
const uglify = require("gulp-uglify"); //压缩模块
const rename = require("gulp-rename"); //重命名


const cleanCss = require("gulp-clean-css"); //压缩css
const babel = require("gulp-babel"); //es6转es5
gulp.task("html", done => {
    gulp.src("*.html").pipe(gulp.dest("dist")).pipe(connect.reload());
    done();
});
gulp.task("js", done => {
    gulp.src("js/**/*").pipe(gulp.dest("dist/js")).pipe(connect.reload());
    done();
});
gulp.task("img", done => {

    gulp.src("imgs/**").pipe(gulp.dest("dist/imgs")).pipe(connect.reload());
    done();
});
gulp.task("sass", done => {
    gulp.src("sass/*.scss")
        .pipe(sourcemaps.init())
        .pipe(sass({
            outputStyle: "compressed"
        }))
        .pipe(sass())
        .pipe(sourcemaps.write("./"))

    .pipe(gulp.dest("dist/css"))
        .pipe(connect.reload());
    done();
});
gulp.task("watch", done => {
    gulp.watch("*.html", gulp.series("html"));
    gulp.watch("sass/*.scss", gulp.series("sass"));
    gulp.watch("js/**", gulp.series("js"))
    gulp.watch("imgs/**", gulp.series("img"))
    done();
});
gulp.task("server", done => {
    connect.server({
        root: "dist",
        livereload: true
    })
    done();
});
gulp.task("concat", done => {
    gulp.src(["js/a.js", "js/b.js"])
        .pipe(concat("main.js"))
        .pipe(gulp.dest("dist/js"))
        .pipe(uglify())

    .pipe(gulp.dest("dist/js"));
    done();
});
//转js
gulp.task("babel", done => {
    gulp.src("js/a.js")
        .pipe(babel({
            presets: ['@babel/preset-env']
        }))
        .pipe(gulp.dest("dist/js"))
    done()
});
gulp.task("default", gulp.series("server", "watch"));